package com.globalLogic.gps.graph;

import java.util.ArrayList;
import java.util.List;

import com.globalLogic.geometry.Point;
import com.globalLogic.gps.maps.adapter.PointNodeAdapter;
import com.globalLogic.gps.maps.elements.Road;

public class MapGraph extends Graph {

	public MapGraph(List<Road> roads) {
		super();
		List<INode> nodes = new ArrayList<INode>();
		for (Road road : roads) 
		{
			for (INode node : road.getNodes())
			{
				if (!nodes.contains(node))
				{
					nodes.add(node);
				}
			}			
		}
		inicializarGraph(nodes);
		for (Road road : roads) 
		{
			this.addRoute(road.getStart().getId(), road.getEnd().getId(), new Double(road.getLength()).intValue());	
		}
	}

	public  String minimumRoute(Point from, Point to)
	{
		return getAlgorithm().minimumRoute(new PointNodeAdapter(from).getId(), new PointNodeAdapter(to).getId());
	}

	public void recalculate(List <Road> roads) {
		List<INode> nodes = new ArrayList<INode>();
		for (Road road : roads) 
		{
			for (INode node : road.getNodes())
			{
				if (!nodes.contains(node))
				{
					nodes.add(node);
				}
			}			
		}
		inicializarGraph(nodes);
		for (Road road : roads) 
		{
			this.addRoute(road.getStart().getId(), road.getEnd().getId(), new Double(road.getLength()).intValue());	
		}

	}




}
